using WebApi.Core.Entities;
using WebApi.Core.Interfaces;

namespace WebApi.Infrastructure.Repositories
{
    public class InMemoryUserRepository : IUserRepository
    {
        private readonly List<User> _users;

        public InMemoryUserRepository()
        {
            // 初始化一些测试用户
            _users = new List<User>
            {
                new User
                {
                    Id = 1,
                    Username = "admin",
                    // 密码是 "123456" 的哈希值
                    Password = BCrypt.Net.BCrypt.HashPassword("123456"),
                    Email = "admin@example.com",
                    CreatedAt = DateTime.Now
                }
            };
        }

        public async Task<User> GetByUsernameAsync(string username)
        {
            return await Task.FromResult(_users.FirstOrDefault(u => u.Username == username));
        }

        public async Task<User> GetByIdAsync(int id)
        {
            return await Task.FromResult(_users.FirstOrDefault(u => u.Id == id));
        }

        public async Task<IEnumerable<User>> GetAllAsync()
        {
            return await Task.FromResult(_users);
        }

        public async Task<User> AddAsync(User entity)
        {
            entity.Id = _users.Max(u => u.Id) + 1;
            _users.Add(entity);
            return await Task.FromResult(entity);
        }

        public async Task UpdateAsync(User entity)
        {
            var index = _users.FindIndex(u => u.Id == entity.Id);
            if (index != -1)
            {
                _users[index] = entity;
            }
            await Task.CompletedTask;
        }

        public async Task DeleteAsync(User entity)
        {
            _users.RemoveAll(u => u.Id == entity.Id);
            await Task.CompletedTask;
        }
    }
} 